<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>星辰诞愿 - 祝福管理</title>
    <style>
        /* Inherit all styles from home.html */
        body {
            margin: 0;
            padding: 0;
            font-family: 'Comic Sans MS', cursive, sans-serif;
            background: linear-gradient(135deg, #fff5f5 0%, #fff0f5 100%);
            min-height: 100vh;
        }
        .nav {
            display: flex;
            align-items: center;
            padding: 15px 30px;
            background: rgba(255,255,255,0.95);
            box-shadow: 0 2px 15px rgba(210, 51, 105, 0.1);
            position: fixed;
            width: 100%;
            top: 0;
            z-index: 1000;
            border-bottom: 3px solid #ff8e9e;
        }
        .nav img {
            height: 50px;
            margin-right: 15px;
        }
        .blog-title {
            font-size: 24px;
            font-weight: bold;
            color: #d23369;
            text-shadow: 1px 1px 2px rgba(0,0,0,0.1);
            position: absolute;
            left: 100px;
        }
        .nav-links {
            display: flex;
            position: absolute;
            left: 50%;
            transform: translateX(-50%);
            background: rgba(255,255,255,0.9);
            padding: 5px 15px;
            border-radius: 30px;
            border: 2px solid #ff8e9e;
            box-shadow: 0 2px 10px rgba(210, 51, 105, 0.2);
        }
        .nav-span {
            margin: 0 10px;
            color: #d23369;
            text-decoration: none;
            font-weight: bold;
            padding: 8px 15px;
            border-radius: 20px;
            transition: all 0.3s;
            border: 2px solid transparent;
        }
        .nav-span:hover {
            background-color: #ffebee;
            color: #ff4081;
            border-color: #ff8e9e;
        }
        .space {
            flex-grow: 1;
        }
        .container {
            display: flex;
            padding-top: 90px;
            min-height: calc(100vh - 90px);
            box-sizing: border-box;
        }
        .management-container {
            width: 90%;
            margin: 0 auto;
            padding: 20px;
        }
        .card {
            background: white;
            border-radius: 15px;
            padding: 20px;
            margin-bottom: 20px;
            box-shadow: 0 5px 15px rgba(210, 51, 105, 0.1);
            border: 1px solid #ffd6de;
            position: relative;
        }
        .card:before {
            content: "🎀";
            position: absolute;
            top: -10px;
            left: -10px;
            font-size: 20px;
        }
        h1 {
            color: #d23369;
            text-align: center;
            margin-bottom: 30px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #ffd6de;
        }
        th {
            background-color: #fff9fa;
            color: #d23369;
            font-weight: bold;
        }
        tr:hover {
            background-color: #fff5f7;
        }
        .action-buttons {
            display: flex;
            gap: 10px;
        }
        .edit-btn, .delete-btn {
            padding: 5px 10px;
            border-radius: 5px;
            border: none;
            cursor: pointer;
            font-weight: bold;
            transition: all 0.3s;
        }
        .edit-btn {
            background-color: #a18cd1;
            color: white;
        }
        .edit-btn:hover {
            background-color: #8a7bbd;
        }
        .delete-btn {
            background-color: #ff6b88;
            color: white;
        }
        .delete-btn:hover {
            background-color: #e05572;
        }
    </style>
</head>
<body>
<div class="nav">
    <img src="pic/cat.jpg" alt="小猫">
    <span class="blog-title">星辰诞愿 - 祝福管理</span>
    <div class="nav-links">
        <a class="nav-span" href="home.html">主页</a>
    </div>
    <div class="space"></div>
</div>
<div class="container">
    <div class="management-container">
        <div class="card">
            <h1>🎂 祝福语管理 🎂</h1>

            <table id="blessings-table">
                <thead>
                <tr>
                    <th>ID</th>
                    <th>祝福内容</th>
                    <th>发布时间</th>
                    <th>发布者ID</th>
                    <th>发布者</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <!-- Data will be loaded here -->
                </tbody>
            </table>
        </div>
    </div>
</div>
<script src="js/jquery.min.js"></script>
<script src="js/common.js"></script>
<script>
    // Load blessings data
    function loadBlessings() {
        $.ajax({
            type: "get",
            url: "/bless/getBlessInfoListByUserId",
            success: function(result) {
                if (result.code === "SUCCESS") {
                    renderTable(result.data);
                } else {
                    alert("加载数据失败: " + result.errorMessage);
                }
            },
            error: function(error) {
                if (error.status === 401) {
                    location.href = "login.html";
                } else {
                    alert("网络错误，请稍后重试");
                }
            }
        });
    }
    // Render table with all blessings
    function renderTable(data) {
        let blessList = data;
        let html = '';
        blessList.forEach(bless => {
            html += `
            <tr>
                <td>${bless.id}</td>
                <td>${bless.content}</td>
                <td>${new Date(bless.createTimeStatus).toLocaleString()}</td>
                <td>${bless.userId}</td>
                <td>${bless.userName}</td>
                <td class="action-buttons">
                    <button class="edit-btn" onclick="editBlessing(${bless.id})">编辑</button>
                    <button class="delete-btn" onclick="deleteBlessing(${bless.id},${bless.userId})">删除</button>
                </td>
            </tr>`;
        });
        $('#blessings-table tbody').html(html);
    }
    // Edit blessing
    function editBlessing(id) {
        const newContent = prompt("请输入新的祝福内容:");
        if (newContent) {
            $.ajax({
                type: "post",
                url: "/bless/update",
                contentType: "application/json",
                data: JSON.stringify({
                    id: id,
                    content: newContent
                }),
                success: function(result) {
                    if (result.code === "SUCCESS") {
                        alert("修改成功!");
                        loadBlessings();
                    } else {
                        alert("修改失败: " + result.errorMessage);
                    }
                },
                error: function (error) {
                    if (error != null && error.status === 401) {
                        location.href = "login.html";
                    }
                }
            });
        }
    }
    //删除祝福
    function deleteBlessing(id,userId){
        if(confirm("确定删除吗？")) {
            $.ajax({
                type: "post",
                url: "bless/delete",
                data: {
                    id: id,
                    userId:userId
                },
                success: function (result) {
                    if (result.code === "SUCCESS") {
                        alert("删除成功");
                        loadBlessings();
                    } else {
                        alert(result.errorMessage);
                    }
                },
                error: function (error) {
                    if (error != null && error.status === 401) {
                        location.href = "login.html";
                    }
                }
            })
        }
    }
    // 初始化页面
    $(document).ready(function() {
        loadBlessings();
    });
</script>
</body>
</html>